/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.saving;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.gl.Tax;
import src.domain.saving.Saving;
import src.domain.saving.Savpurpose;
import src.services.gl.TaxModel;
import src.services.saving.SavingModel;
import src.services.saving.SavpurposeModel;
import src.util.Constanta;
import src.util.Messager;

/**
 *
 * @author jasuke
 */
@ManagedBean
@SessionScoped
public class SavingInfoBean {

    private List<Savpurpose> avaiblePurpose;
    private Saving saving;
    private List<Saving> savings;
    private SavpurposeModel purHelper;
    private SavingModel helper;
    private String filter;
                
    private boolean editMode = false;
    
    public SavingInfoBean() {
        this.purHelper    = new SavpurposeModel();
        this.helper       = new SavingModel();        
    }

    public String getFilter() {
        return filter;
    }

    public void setFilter(String filter) {
        this.filter = filter;
    }

    public void handleClose(CloseEvent event) {
        this.saving = null;
        
    }
    public void search() {
        StringBuilder sqlFull = new StringBuilder();
        sqlFull.append("from Saving sav ");                
        sqlFull.append("where closeDate is null ");
        sqlFull.append("and closedBy is null ");
        
        if (!filter.equals(""))
             sqlFull.append(" and (upper(customer.customerName) like '%").append(filter).append("%' or ").append(" accountNo like '%").append(filter).append("%') ");
        sqlFull.append("order by accountNo");
        this.savings = helper.search(sqlFull.toString());
    }
    
    public Saving getSaving() {
        return saving;
    }

    public void setSaving(Saving saving) {
        this.saving = saving;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public List<Saving> getSavings() {
        return savings;
    }

    public List<Savpurpose> getAvaiblePurpose() {
        return avaiblePurpose;
    }
        
    public Double getInterestTaxName(Integer id) {  
        TaxModel tm = new TaxModel();
        Double txRate = null;
        Tax tax = tm.getById(id);
        
        if (tax != null) txRate = tax.getTaxRate().doubleValue();
        return txRate;
    }
    
    private void initForm() {
        this.avaiblePurpose = purHelper.findAll(null);
        this.editMode = true;
    }
    
    public void openData() {
        initForm();
    }
    
    public void loadSavings () {
        Map<String, String> savfilter = new HashMap<>();
      //  savfilter.put("approvalUid", null);
        savfilter.put("closeDate", null);
        savfilter.put("closedBy", null);
        savfilter.put("order", "accountNo");
                
        this.savings = helper.findAll(savfilter);
    }
    
    public void approve(boolean approved, Integer userId) {
        RequestContext context = RequestContext.getCurrentInstance();
        boolean success;  
        
        if (approved==true) saving.setApprovalUid(userId);
        else {
            saving.setClosedBy(Constanta.SAV_CLOSED_BY_SYSTEM);
            saving.setCloseDate(new Date());
        }
        
        success = helper.saveData(saving);
        
        if (success) {
            this.savings.remove(this.saving);
            this.saving = null;
        } 
        
        Messager.showSaveUpdateStatus(success);     
        context.addCallbackParam("success", success);  
    }
}
